%%%%%%%%%%%% ART PARAMETERS (edit to desired values) %%%%%%%%%%%%
global_mean=1;              % global mean type (1: Standard 2: Every Voxel 3: User Mask 4: Auto)
motion_file_type=0;         % motion file type (0: SPM .txt file 1: FSL .par file 2:Siemens .txt file)
global_threshold=3;         % threshold for outlier detection based on global signal
motion_threshold=.6;         % threshold for outlier detection based on motion estimates
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
STEPS=[1,1];

if STEPS(1),
    files=spm_select(Inf,'SPM\.mat','Select SPM.mat files (one per subject)');
    for n1=1:size(files,1),
        cfgfile=fullfile(pwd,['art_config',num2str(n1,'%03d'),'.cfg']);
        fid=fopen(cfgfile,'wt');
        [filepath,filename,fileext]=fileparts(deblank(files(n1,:)));
        load(deblank(files(n1,:)),'SPM');
        
        fprintf(fid,'# Automatic script generated by %s\n',mfilename);
        
        fprintf(fid,'sessions: %d\n',length(SPM.Sess));
        fprintf(fid,'global_mean: %d\n',global_mean);
        fprintf(fid,'global_threshold: %f\n',global_threshold);
        fprintf(fid,'motion_threshold: %f\n',motion_threshold);
        fprintf(fid,'motion_file_type: %d\n',0);
        fprintf(fid,'motion_fname_from_image_fname: 1\n');
        fprintf(fid,'spm_file: %s\n',deblank(files(n1,:)));
        fprintf(fid,'end\n');
        
        for n2=1:length(SPM.Sess),
            temp=[SPM.xY.P(SPM.Sess(n2).row,:),repmat(' ',[length(SPM.Sess(n2).row),1])]';
            fprintf(fid,'session %d image %s\n',n2,temp(:)');
        end
        fprintf(fid,'end\n');
        fclose(fid);
    end
end

if STEPS(2),
    for n1=1:size(files,1),
        cfgfile=fullfile(pwd,['art_config',num2str(n1,'%03d'),'.cfg']);
        disp(['running subject ',num2str(n1),' using config file ',cfgfile]);
        art('sess_file',cfgfile);
        set(gcf,'name',['art_batch: art subject #',num2str(n1)]);
    end
end

